package net.entelijan.cobean.examples.showcase.table;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.IMultiSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.table.DefaultTableInitializer;
import net.entelijan.cobean.util.CobeanUtil;
import net.entelijan.cobean.util.DateConverter;
import net.entelijan.scf.IShowcase;

public class FilterKeyTableShowcase implements IShowcase<FilterKeyTablePanel> {

	private ILitService service = null;

	public FilterKeyTableShowcase() {
		super();
	}

	@Override
	public void init(final FilterKeyTablePanel panel) {

		// Create an initializer
		DefaultTableInitializer<LitAuthor> initializer = new DefaultTableInitializer<LitAuthor>();
		initializer.setCoTable(panel.getTable());
		initializer.setCoFilterInput(panel.getFilterTextField());
		// Add some configurations.
		initializer.setColumnDescs(createColumnDescriptions());

		// Init the components and receive the component bean.
		// From now on the component bean is the only interface to the
		// 'real' components.
		final IMultiSelectionListCobean<LitAuthor> cobean = initializer
				.initialize();

		// Add some data to the compnent bean
		final List<LitAuthor> data = service.loadAllAuthors();
		cobean.setValue(data);

		// Now the locales are displayed in the component as defined in the
		// initializer

		// Add an action to the 'Show Selection' button
		panel.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, ExamplesUtil.listToString(
						cobean.getSelectedValue(), 80));
			}

		});

		// Add an action to the 'Delete Selection' button
		panel.getDeleteSelectionButton().addActionListener(
				new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						CobeanUtil.removeSelected(cobean);
					}

				});

	}

	/**
	 * Create some column descriptions for java.util.Local
	 * 
	 * @return A list of column descriptions
	 */
	private List<ColumnDesc> createColumnDescriptions() {
		List<ColumnDesc> colDescs = new ArrayList<ColumnDesc>();
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("name");
			colDesc.setHeaderName("Name(filterable)");
			colDesc.setFilterable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("language");
			colDesc.setHeaderName("Language(sortable)");
			colDesc.setSortable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("country");
			colDesc.setHeaderName("Country(sortable)");
			colDesc.setSortable(true);
			colDescs.add(colDesc);
		}
		{
			ColumnDesc colDesc = new ColumnDesc();
			colDesc.setPropertyName("birthday");
			colDesc.setHeaderName("Birthday");
			colDesc.setConverter(new DateConverter("yyyy-MM-dd"));
			colDescs.add(colDesc);
		}
		return colDescs;
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
